let http = require("http");
let server = http.createServer();
let os = require('os');

console.log(os.cpus());
console.log(os.totalmem());



server.on("request",function(request,response){
	console.log("someone come");
	console.log("请求路径:"+request.url);
	console.log("远程请求的 ip:"+request.socket.remoteAddress);
	console.log("远程请求的 port:"+request.socket.remotePort);
	//response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
	response.setHeader("Content-Type","text/html;charset=utf-8");
	
	if(request.url === '/login'){
		response.write('登录');
	}else if(request.url === '/products'){
		let products = [{
			name:'iphone X',
			price:8888
		},{
			name:'iphone 7 plus',
			price:5999
		}];
		response.write(JSON.stringify(products));
	}else{
		response.write("404");
	}
	response.end();

	//可以使用 response.end('产品列表') 代替 写 write 和 end
});

server.listen(8080,function () {
	console.log("server started please visit http://127.0.0.1:8080");
});
